var app = getApp();
var util = require("../../../../utils/util.js");
Page({
  data: {
    message: [], //产品具体信息
    messagePL: [], //评论
    inputtext: "", //input框
    bpname: "", //回复哪一个人
    huifuText: false, //回复
    focus: false, //聚焦
    zhuan: "", //转发次数
  },
  onLoad: function(e) {
    var openid = wx.getStorageSync('openid');
    var name = wx.getStorageSync('name');
    var touxiang = wx.getStorageSync('touxiang');
    var uid = wx.getStorageSync('uid');
    var that = this;
    this.setData({
      openid: openid,
      name: name,
      touxiang: touxiang,
      uid: uid,
      e: e
    });
    //获取产品具体信息
    wx.request({
      url: 'https://www.mamingwang.cn/api/openid/chakan',
      data: {
        openid: openid,
        mid: e.mid,
        uid: e.uid
      },
      method: "post",
      success(res) {
        console.log(res)
        //时间
        let d = new Date(res.data.data.createtime.replace(/-/g, '/')).getTime();
        res.data.data.createtime = util.timeHandle(d);
        that.setData({
          message: res.data.data,
          num2: res.data.data.num2,
          zhuan: res.data.data.shu
        })
      }
    });
    //获取评论
    wx.request({
      url: 'https://www.mamingwang.cn/api/openid/pinglun',
      data: {
        openid: openid,
        mid: e.mid,
        uid: e.uid
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data.data != "abcd") {
          for (var i = 0; i < res.data.data.length; i++) {
            //时间
            var d = new Date(res.data.data[i].create_time.replace(/-/g, '/')).getTime();
            res.data.data[i].create_time = util.timeHandle(d);
            if (res.data.data[i].down) {
              for (var j = 0; j < res.data.data[i].down.length; j++) {
                var a = new Date(res.data.data[i].down[j].create_time.replace(/-/g, '/')).getTime();
                res.data.data[i].down[j].create_time = util.timeHandle(a);
              }
            }
          }
          that.setData({
            messagePL: res.data.data
          })
        }
      }
    })
  },
  onShow: function() {
    this.onLoad(this.data.e)
  },
  //打电话
  call: function() {
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/bohao',
      data: {
        openid: this.data.openid,
        mid: this.data.message.mid
      },
      method: "post",
      success(res) {
        console.log(res.data)
        if (res.data.code == -3) {
          wx.showToast({
            title: '今日拨号次数已达上限',
            icon: "none",
            duration: 1500,
            mask:true
          })
        } else {
          wx.makePhoneCall({
            phoneNumber: that.data.message.phone
          })
        }
      }
    })
  },
  //转发
  onShareAppMessage: function() {
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/zhuanfa',
      data: {
        openid: this.data.openid,
        mid: this.data.message.mid
      },
      method: "post",
      success(res) {
        console.log(that.data.zhuan)
        return {
          title: '转发',
          path: '/pages/jutixiangqing/chanpinjuti/chanpin?mid=' + that.data.message.mid,
          imageUrl: that.data.message.picture[0],
          success: function(res) {}
        }
        that.setData({
          zhuan: that.data.zhuan + 1
        })
      }
    })
  },
  //评论聚焦
  pinglunfocus: function() {
    this.setData({
      focus: true
    })
  },
  //评论
  pinglun: function(e) {
    wx.showToast({
      title: '上传中',
      icon: "none",
      duration: 1500,
      mask: true
    })
    var that = this;
    var openid = wx.getStorageSync('openid');
    if (e.detail.value.content.length < 50) {
      if (e.detail.value.content == "") {
        wx.showToast({
          title: '评论内容不能为空',
          icon: "none",
          duration: 1500,
          mask:true
        })
      } else {
        if (this.data.evalute_id) {
          wx.request({
            url: 'https://www.mamingwang.cn/api/xinxi/pinglunadd',
            data: {
              openid: this.data.openid,
              mid: this.data.message.mid,
              evalute_id: this.data.evalute_id,
              content: e.detail.value.content
            },
            method: "post",
            success(res) {
              that.onLoad(that.data.e);
              that.setData({
                huifuText: false,
                bpname: "",
                focus: false,
                evalute_id: "",
                inputtext:""
              })
              wx.showToast({
                title: '回复成功',
                icon: "none",
                duration: 1000,
                mask: true
              })
            }
          })
        } else {
          wx.request({
            url: 'https://www.mamingwang.cn/api/xinxi/pinglunad',
            data: {
              openid: this.data.openid,
              mid: this.data.message.mid,
              content: e.detail.value.content,
            },
            method: "post",
            success(res) {
              that.onLoad(that.data.e);
              that.setData({
                inputtext:""
              })
              wx.showToast({
                title: '评论成功',
                icon: "none",
                duration: 1000,
                mask: true
              })
            }
          })
        }
      }
    } else {
      wx.showToast({
        title: '评论字数不能超过50个字',
        icon: "none",
        duration: 1500,
        mask:true
      })
    }
  },
  //回复
  huifu: function(e) {
    this.setData({
      bpname: e.currentTarget.dataset.pjname,
      huifuText: true,
      focus: true,
      evalute_id: e.currentTarget.dataset.txtid,
    })
  },
  //取消回复
  quxiaohuifu: function() {
    this.setData({
      huifuText: false,
      bpname: "",
      focus: false,
      evalute_id:""
    })
  },
  //点赞
  dianzan: function() {
    var that = this;
    wx.showToast({
      title: '请稍后',
      icon: 'loading',
      duration: 1000,
      mask: true,
    })
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/dianzan',
      data: {
        openid: this.data.openid,
        mid: this.data.message.mid
      },
      method: "post",
      success(res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1000,
          mask: true,
        })
        console.log(res)
        that.data.message.dian = 1;
        that.data.message.like.push({
          uid: that.data.uid,
          uname: that.data.name
        })
        that.setData({
          message: that.data.message
        })
      }
    })
  },
  //取消点赞
  quxiaodianzan: function() {
    var that = this;
    wx.showToast({
      title: '请稍后',
      icon: 'loading',
      duration: 1000,
      mask: true,
    })
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/quxiao',
      data: {
        openid: this.data.openid,
        mid: this.data.message.mid
      },
      method: "post",
      success(res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1000,
          mask: true,
        })
        console.log(res)
        for (var i = 0; i < that.data.message.like.length; i++) {
          if (that.data.message.like[i].uid == that.data.uid) {
            that.data.message.like.splice(i, 1);
          }
        }
        that.data.message.dian = 0;
        that.setData({
          message: that.data.message
        })
      }
    })
  },
  //关注
  jiaguanzhued: function() {
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/focus',
      data: {
        openid: this.data.openid,
        aid: this.data.message.uid
      },
      method: "post",
      success(res) {
        console.log(res)
        that.data.message.fans = 1;
        that.setData({
          message: that.data.message
        })
      }
    })
  },
  //取消关注
  quxiaoguanzhu: function() {
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/nofocus',
      data: {
        openid: this.data.openid,
        aid: this.data.message.uid
      },
      method: "post",
      success(res) {
        that.data.message.fans = 0;
        that.setData({
          message: that.data.message
        })
      }
    })
  },
  //跳转到委托采购
  goWeiTuo: function() {
    wx.navigateTo({
      url: '../../shangquan/weiTuo/weiTuo',
    })
  },
  //跳转到金融服务
  goJinRong: function() {
    wx.navigateTo({
      url: '../../shangquan/jinrong/suiNiYa/suiNiYa',
    })
  },
  //跳转到名片
  goMingPian: function(e) {
    if (e.currentTarget.dataset.uid == this.data.uid) {
      wx.switchTab({
        url: '../../myID/myID',
      })
    } else {
      wx.navigateTo({
        url: '../../mingpian/mingpian?uid=' + e.currentTarget.dataset.uid,
      })
    }
  },
  //转发限制
  zhuanfaxianzhi: function() {
    wx.showToast({
      title: '今日转发次数已达上限',
      icon: "none",
      duration: 1500,
      mask:true
    })
  }
})